1. Field of the Invention
The present invention relates to a method, system, and program for implementing conditional logic and, in particular, nested conditional logic within a computer program.
2. Description of the Related Art
Certain complex program coding requires the use of multiple nested conditional logic. Below is an example (1) of the use of nested conditional statements to determine whether a process will continue to request blocks of data.
Get a blockxe2x80x83xe2x80x83(1)
If block OK Then
Get another block
If block OK Then
Get another block
If block OK Then
Get another block
If block OK Then
xe2x80x83say hello
Else
xe2x80x83say goodbye
Else
Error
Else
Error
Else
Error
In the above example (1), the programmer must keep track of numerous nested conditional statements in order to implement a series of commands to get blocks of data,
as a failure to get one block will prevent the logic from obtaining the next block. The programmer must also ensure that the xe2x80x9cElsexe2x80x9d statements correspond to xe2x80x9cIfxe2x80x9d statements. The layout of the xe2x80x9cifxe2x80x9d and xe2x80x9celsexe2x80x9d statements to implement nested conditional logic can be confusing and time consuming to organize.
Thus, there is a need in the art for an improved method, system, and program for implementing conditional logic.
To address the shortcomings in the prior art discussed above, preferred embodiments disclose a method, system, and program for implementing conditional logic statements within a computer program. Program logic is invoked in response to processing a first method statement. During the execution of the program logic for the first method statement, at least one operation is performed and a return variable is set to at least one of two values depending on an outcome of performing the at least one operation. Program logic is invoked in response to processing a second method statement following the first method statement in the computer program. The second method statement is passed a condition parameter and the return variable. During the execution of the program logic for the second method statement, a determination is made as to whether the return variable set by the first method statement satisfies a condition specified in the condition parameter and one of at least two operations are performed, depending on whether the return variable satisfies the condition.
In further embodiments, the step of performing one of at least two operations is dependent on whether the return variable satisfies the condition comprises performing at least one operation specified in the program logic for the second method statement if the return variable satisfies the condition or bypassing performing any operations specified in the second method statement if the return variable does not satisfy the condition.
In still further embodiments, the computer program further comprises additional method statements following the second method statement. The method statements following the second program statement are passed at least one condition parameter and the return variable. During the execution of the program logic for the method statements following the first method statement, at least one operation is performed depending on whether the return variable satisfies the condition specified in the at least one condition parameter and the return variable is set to one of the two values depending on the outcome of performing the operation.
In still further embodiments, the same operations are performed by the method statements following the first method statement and the same condition parameters are passed to the method statements following the first method statement. Still further, the series of method statements may form nested conditional logic.
Preferred embodiments provide a technique to implement conditional logic that is less cumbersome than current methods that utilize xe2x80x9cifxe2x80x9d and xe2x80x9celsexe2x80x9d statement pairs, especially when numerous if/else pairs are nested within each other. With preferred embodiments, the conditional logic is implemented within a method statement that performs a certain operation conditional on specified condition parameters and a return value set during the execution of a previous method statement. In this way, nested conditional logic can be implemented with a series of single program statements without the need to organize the layout of xe2x80x9cifxe2x80x9d and xe2x80x9celsexe2x80x9d pairs within each other.